package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.entity.User;
import com.xishu.entity.plat.PrivilegeMapping;
import com.xishu.entity.plat.UserCompanyMapping;
import com.xishu.entity.plat.UserShopMapping;
import com.xishu.entity.shop.Shop;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.MappingService;
import com.xishu.service.ShopService;
import com.xishu.util.ServletUtil;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(description = "权限组")
@RestController
public class PrivilegeMappingController implements Tools {
    private CommonService commonService = CommonService.getInstance();

    @ApiOperation(value = "创建权限组")
    @PostMapping(value = ("/privilege/mapping"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkCompany = true)
    public ResponseData create(@RequestBody PrivilegeMapping privilegeMapping) throws Exception {
        return commonService.createObject(privilegeMapping);
    }

    @ApiOperation(value = "修改权限组")
    @PutMapping(value = ("/privilege/mapping/modify"), produces = ("application/json;charset=UTF-8"))
    public ResponseData modify(@RequestBody PrivilegeMapping privilegeMapping) throws Exception {
        User user = ServletUtil.getUser();

        if (!getBoolean(user.getAdmin())) {
            //权限校验
            PrivilegeMapping mappingInDb = CommonService.getInstance().findEntity(privilegeMapping.getId(), PrivilegeMapping.class);
            Long companyId = mappingInDb.getCompanyId();

            if (companyId != null) {
                if (getBoolean(user.getPlatManager()) || getBoolean(user.getCompanyManager())) {
                    List<UserCompanyMapping> mappingList = MappingService.getInstance().findUserCompanyMappingByUserId(user.getId());
                    final Optional<UserCompanyMapping> findCompanyOptional = mappingList.stream().filter(p -> equals(p.getCompanyId(), companyId)).findAny();
                    VerifyUtil.verify(() -> findCompanyOptional.isPresent(), ResponseStatus.PRIVILEGE);
                } else if (getBoolean(user.getShopManager())) {
                    List<UserShopMapping> mappingList = MappingService.getInstance().findUserShopMappingByUserId(user.getId());
                    VerifyUtil.verify(() -> isNotEmpty(mappingList), ResponseStatus.PRIVILEGE);
                    UserShopMapping userShopMapping = mappingList.get(0);
                    Long shopId = userShopMapping.getShopId();
                    Shop shop = ShopService.getInstance().findShopById(shopId);
                    VerifyUtil.verify(() -> equals(shop.getCompanyId(), companyId), ResponseStatus.PRIVILEGE);
                } else {
                    VerifyUtil.throwError(ResponseStatus.PRIVILEGE);
                }
            }
        }

        return commonService.modifyObject(privilegeMapping);
    }

    @ApiOperation(value = "删除权限组")
    @DeleteMapping(value = ("/privilege/mapping"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkCompany = true)
    public ResponseData delete(@RequestBody PrivilegeMapping privilegeMapping) throws Exception {

        return commonService.delete(privilegeMapping);
    }

    @ApiOperation(value = "查询权限组")
    @PutMapping(value = ("/privilege/mapping"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody PrivilegeMapping privilegeMapping) throws Exception {
        return commonService.searchResponse(privilegeMapping);
    }


}
